# -*- coding:utf-8 -*-
from fastapi import APIRouter, Request, Depends, Query
from core.curd import PageInfo, IDList
from core.response import tableinfo, success, fail
from core.request import get_page_info, get_query_param
from app.annotation import RequiresPermissions, FieldsFilter
from app.service.system import SysUserService
from app.entity.system import CreateSysUser, UpdateSysUser
from app.annotation import Log

router = APIRouter()


@router.get('/get')
@RequiresPermissions('sys:user:detail')
async def get(ids: int = Query(), mService=Depends(SysUserService.instance)):
    """
        获取测试详情
    """
    one = await mService.select_by_id(ids)
    if not isinstance(one, dict):
        one = one.__dict__
    del one['password']
    del one['salt']

    one['roleIdList'] = await mService.get_user_role(ids)
    one['postIdList'] = await mService.get_user_post(ids)

    return success('OK', one)


@router.get('/list')
@RequiresPermissions('sys:user:list')
@FieldsFilter(fields=['password', 'salt'])
async def lists(req: Request, page=Depends(get_page_info), where=Depends(get_query_param),
                mService=Depends(SysUserService.instance)):
    """
        获取测试列表
    """
    total, rows = await mService.select_page(page, where)
    return tableinfo('OK', total, rows)


@router.post('/add')
@RequiresPermissions('sys:user:add')
async def add(post: CreateSysUser, mService=Depends(SysUserService.instance)):
    """
        添加测试
    """
    one = await mService.create_user(post.dict())
    return success('OK')


@router.post('/edit')
@RequiresPermissions('sys:user:edit')
async def update(post: UpdateSysUser, mService=Depends(SysUserService.instance)):
    """
        编辑测试
    """
    one = await mService.update_user(post.dict())
    return success('OK', one)


@router.post('/del')
@RequiresPermissions('sys:user:remove')
async def remove(ids: IDList, mService=Depends(SysUserService.instance)):
    """
        删除测试
    """
    one = await mService.delete_user(ids.ids)
    return success('OK')


@router.get('/getInfo')
async def get_info(req: Request, mService=Depends(SysUserService.instance)):
    user = req.app.state.user

    one = await mService.select_by_id(user['id'])
    if not isinstance(one, dict):
        one = one.__dict__
    del one['password']
    del one['salt']

    one['deptInfo'] = await mService.get_user_dept_info(user['id'])
    one['postInfo'] = await mService.get_user_post_info(user['id'])
    one['roleInfo'] = await mService.get_user_role_info(user['id'])
    return success("OK", {"user": one, "permissions": "*:*:*", "roles": [v['name'] for v in one['roleInfo']]})


@router.get('/info')
@Log("xxxx", business_type=1)
async def info(req: Request, mService=Depends(SysUserService.instance)):
    """
        导入测试列表
        {'id': 1, 'username': 'admin', 'real_name': 'admin', 'avatar': 'https://cdn.maku.net/images/avatar.png',
        'gender': 0, 'email': 'babamu@126.com', 'mobile': '13612345678', 'org_id': 14, 'status': 1}
    """
    user = req.app.state.user

    one = await mService.select_by_id(user['id'])
    if not isinstance(one, dict):
        one = one.__dict__
    del one['password']
    del one['salt']

    one['deptInfo'] = await mService.get_user_dept_info(user['id'])
    one['postInfo'] = await mService.get_user_post_info(user['id'])
    one['roleInfo'] = await mService.get_user_role_info(user['id'])

    return success('OK', one)


@router.post('/import')
@RequiresPermissions('sys:user:import')
async def imports(req: Request, mService=Depends(SysUserService.instance)):
    """
        导入测试列表
    """

    return success('OK')


@router.post('/export')
@RequiresPermissions('sys:user:export')
async def export(req: Request, mService=Depends(SysUserService.instance)):
    """
        导出测试列表
    """

    return success('OK')
